package com.koron.car.web.applycar.service.impl;

import com.koron.car.config.Constants;
import com.koron.car.web.applycar.bean.entity.DriverAccident;
import com.koron.car.web.applycar.bean.query.DriverAccidentQuery;
import com.koron.car.web.applycar.mapper.DriverAccidentMapper;
import com.koron.car.web.applycar.service.DriverAccidentService;
import com.koron.car.web.common.CommonUtils;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * (DriverAccident)表服务实现类
 *
 * @author twcao
 * @since 2022-03-09 10:37:08
 */
@Service
public class DriverAccidentServiceImpl implements DriverAccidentService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public DriverAccident queryById(SessionFactory factory, String id) {
        DriverAccidentMapper mapper = factory.getMapper(DriverAccidentMapper.class);
        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("getByApplyId")
    public List<DriverAccident> getByApplyId(SessionFactory factory, String applyId) {
        DriverAccidentMapper mapper = factory.getMapper(DriverAccidentMapper.class);
        return mapper.getByApplyId(applyId);
    }

    /**
     * 查询多条数据
     *
     * @param query 查询条件
     * @return 对象列表
     */
    @Override
    @TaskAnnotation("queryList")
    public List<DriverAccident> queryList(SessionFactory factory, DriverAccidentQuery query) {
        DriverAccidentMapper mapper = factory.getMapper(DriverAccidentMapper.class);
        return mapper.queryList(query);
    }

    /**
     * 新增数据
     * @param accident 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("insert")
    public DriverAccident insert(SessionFactory factory, DriverAccident accident) {
        DriverAccidentMapper mapper = factory.getMapper(DriverAccidentMapper.class);
        Assert.notNull(accident, "出险信息不能为空");
        Assert.hasText(accident.getApplyId(), "申请信息不能为空");
        CommonUtils.update(accident, ThreadLocalContext.get());
        accident.setId(CodeTools.getCode32());
        accident.setState(Constants.AccidentState.DRAFT);
        mapper.insert(accident);
        return accident;
    }

    /**
     * 修改数据
     *
     * @param accident 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("update")
    public DriverAccident update(SessionFactory factory, DriverAccident accident) {
        DriverAccidentMapper mapper = factory.getMapper(DriverAccidentMapper.class);
        CommonUtils.update(accident, ThreadLocalContext.get());
        if(accident.getSubmit()) {
            accident.setState(Constants.AccidentState.FINISH);
        }
        mapper.update(accident);
        return this.queryById(factory, accident.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        DriverAccidentMapper mapper = factory.getMapper(DriverAccidentMapper.class);
        return mapper.deleteById(id) > 0;
    }
}
